package com.jsu.config;


import org.springframework.amqp.core.*;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitMqConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();//用json序列化转化队列消息
    }

    //声明1个路由key 1个队列 1个交换机
    //延迟交换机
    public static final String DELAY_EXCHANGE_NAME = "delay.exchange";
    //延迟队列
    public static final String DELAY_QUEUE_NAME = "delay.queue";
    //延迟队列路由key
    public static final String DELAY_QUEUE_ROUTING_KEY = "delay.queue.routingkey";
    public static final String FANOUT_QUEUE_NAME = "live.queue";
    public static final String FANOUT_QUEUE_ADMIN = "admin.queue";



    // 声明延迟交换机,由插件处理代码
    @Bean("delayExchange")
    public CustomExchange delayExchange() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        // 自定义的交换机
        return new CustomExchange(DELAY_EXCHANGE_NAME,
                "x-delayed-message", true, false, args);
    }

    // 声明延迟队列,
    @Bean("delayQueue")
    public Queue delayQueueA() {
        return new Queue(DELAY_QUEUE_NAME);
    }

    //声明直播订阅通知队列
    @Bean("liveQueue")
    public Queue liveQueue(){
        return new Queue(FANOUT_QUEUE_NAME);
    }

    @Bean("adminQueue")
    public Queue adminQueue(){
        return new Queue(FANOUT_QUEUE_ADMIN);
    }

    // 声明延迟队列B的绑定关系
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueue") Queue queue,
                                 @Qualifier("delayExchange") CustomExchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with(DELAY_QUEUE_ROUTING_KEY)
                .noargs();
    }

   /* @Bean
    public Binding liveBingding(@Qualifier("liveExchange") FanoutExchange exchange,
                                @Qualifier("liveQueue") Queue queue){
        return BindingBuilder.bind(queue)
                .to(exchange);
    }*/

}
